# 部分弹幕功能代码来自项目：https://github.com/IsoaSFlus/danmaku，感谢大佬
# 快手弹幕代码来源及思路：https://github.com/py-wuhao/ks_barrage，感谢大佬
# 仅抓取用户弹幕，不包括入场提醒、礼物赠送等。
import re
import asyncio
import danmaku
import multiprocessing
from multiprocessing import Pool


def do(url):
    asyncio.run(main(url))

async def printer(q,url):
    while True:
        m = await q.get()
        if m['msg_type'] == 'danmaku':
            p1 = re.compile(r'[.](.*?)[.]', re.S)
            lam = lambda x : re.findall("\d+",x)[1] if re.findall("\d+",x)[0] == '163' else re.findall("\d+",x)[0]
            print(re.findall(p1, url)[0] + lam(url) + f'{m["name"]}：{m["content"]}')
            with open(re.findall(p1, url)[0] + lam(url) + '.txt', 'a', encoding='utf-8') as f:
                f.writelines(f'{m["name"]}：{m["content"]}' + '\n')

async def main(url):
    q = asyncio.Queue()
    dmc = danmaku.DanmakuClient(url, q)
    asyncio.create_task(printer(q,url))
    await dmc.start()


# def a():
#     a = 'https://www.douyu.com/82088'
#     asyncio.run(main(a))
#
# def b():
#     b = 'https://www.douyu.com/9999'
#     asyncio.run(main(b))
#
# def c():
#     c = 'https://www.douyu.com/9392000'
#     asyncio.run(main(c))
#
# def d():
#     d = 'https://www.douyu.com/2988'
#     asyncio.run(main(d))
#
# def e():
#     e = 'https://www.douyu.com/434213'
#     asyncio.run(main(e))
#
# def f():
#     f = 'https://www.douyu.com/1034713'
#     asyncio.run(main(f))
#
# def g():
#     g = 'https://www.douyu.com/5063899'
#     asyncio.run(main(g))
#
# def h():
#     h = 'https://www.douyu.com/7675562'
#     asyncio.run(main(h))
#
# def i():
#     i = 'https://www.douyu.com/68068'
#     asyncio.run(main(i))


if __name__ == '__main__':
    # multiprocessing.Process(target=a).start()
    # multiprocessing.Process(target=b).start()
    # multiprocessing.Process(target=c).start()
    # multiprocessing.Process(target=d).start()
    # multiprocessing.Process(target=e).start()
    # multiprocessing.Process(target=f).start()
    # multiprocessing.Process(target=g).start()
    # multiprocessing.Process(target=h).start()
    # multiprocessing.Process(target=i).start()
    # name_list = ["https://www.douyu.com/9063048",
    #              "https://www.douyu.com/1854725",
    #              "https://www.douyu.com/2690605",
    #              "https://www.douyu.com/7675562",
    #              "https://www.douyu.com/9745028",
    #              "https://www.douyu.com/5651892",
    #              "https://www.huya.com/880351",
    #              "https://www.huya.com/12345",
    #              "https://www.huya.com/142591",
    #              "https://www.huya.com/51268",
    #              "https://www.huya.com/125393",
    #              "https://cc.163.com/359740686/",
    #              "https://cc.163.com/349766831/",
    #              "https://cc.163.com/360092252/",
    #              "https://cc.163.com/350179841/",
    #              "https://cc.163.com/369736392/",
    #              "https://cc.163.com/369331977/",
    #              "https://cc.163.com/348387102/",
    #              "https://cc.163.com/359321656/",
    #              "https://www.huajiao.com/l/321355888?qd=hu",
    #              "https://www.huajiao.com/l/321360958?qd=hu",
    #              "https://www.inke.cn/liveroom/index.html?uid=130321822&id=1619002616672539",
    #              "http://star.longzhu.com/y198835",
    #              "http://star.longzhu.com/m178173",
    #              "http://star.longzhu.com/m183214",
    #              "https://v.laifeng.com/8121115",
    #              "https://look.163.com/live?id=253937357"]
    

    # 全部
    name_list = [
        "https://www.huya.com/880351",
        "https://www.huya.com/283698",
        "https://www.huya.com/397170",
        "https://www.huya.com/415793",
        "https://www.huya.com/821511",
        "https://www.huya.com/224259",
        "https://www.huya.com/56620",
        "https://www.huya.com/24043390",
        "https://www.douyu.com/7675562",
        "https://www.douyu.com/2484461",
        "https://www.douyu.com/968987",
        "https://www.douyu.com/8964416",
        "https://www.douyu.com/9047430",
        "https://www.douyu.com/6554231",
        "https://www.huya.com/713049",
        "https://www.huya.com/854727",
        "https://www.huya.com/919347",
        "https://www.huya.com/23870637",
        "https://www.huya.com/391988",
        "https://www.huya.com/537060",
        "https://www.huya.com/422939",
        "https://www.huya.com/930343",
        "https://www.huya.com/517518",
        "https://www.huya.com/511290",
        "https://www.huya.com/237501",
        "https://www.huya.com/403054",
        "https://www.huya.com/428474",
        "https://www.huya.com/214352",
        "https://www.huya.com/51200",
        "https://www.huya.com/24518195",
        "https://www.huya.com/831051",
        "https://www.huya.com/463214",
        "https://www.huya.com/385602",
        "https://www.huya.com/224300",
        "https://www.huya.com/528512",
        "https://www.huya.com/20778135",
        "https://www.douyu.com/6515025",
        # "https://www.douyu.com/22222",
        "https://www.douyu.com/700",
        "https://www.douyu.com/4624967",
        "https://www.huya.com/23653301",
        "https://www.huya.com/177330",
        "https://www.huya.com/139236",
        "https://www.huya.com/159409",
        "https://www.huya.com/612916",
        "https://www.huya.com/457514",
        "https://www.huya.com/145134",
        "https://www.huya.com/520025",
        "https://www.huya.com/714220",
        "https://www.huya.com/24305630",
        "https://www.huya.com/222471",
        "https://www.huya.com/365398",
        "https://www.huya.com/738241",
        "https://www.huya.com/24381412",
        "https://www.huya.com/24792249",
        "https://www.huya.com/317590",
        "https://www.huya.com/217833",
        "https://www.huya.com/480283",
        "https://www.huya.com/294157",
        "https://www.huya.com/149211",
        "https://www.huya.com/149211",
        "https://www.huya.com/24139606",
        "https://www.huya.com/25093848",
        "https://www.huya.com/20942420",
        "https://www.huya.com/412073",
        "https://www.huya.com/818341",
        "https://www.huya.com/181842",
        "https://www.huya.com/291281",
        "https://www.huya.com/25057486",
        "https://www.huya.com/365398",
        "https://www.huya.com/821511",
        "https://www.huya.com/610711",
        "https://www.huya.com/25093848",
        


    ]





    pool = Pool(len(name_list))  # 制定要开启的进程数, 限定了进程上限
    pool.map(do, name_list)
    pool.close()
    pool.join()


# 虎牙直播：https://www.huya.com/11352915
# 斗鱼直播：https://www.douyu.com/85894
# B站直播：https://live.bilibili.com/70155
# 快手直播：https://live.kuaishou.com/u/jjworld126
# 火猫直播：
# 企鹅电竞：https://egame.qq.com/383204988
# 花椒直播：https://www.huajiao.com/l/303344861?qd=hu
# 映客直播：https://www.inke.cn/liveroom/index.html?uid=87493223&id=1593906372018299
# CC直播：https://cc.163.com/363936598/
# 酷狗直播：https://fanxing.kugou.com/1676290
# 战旗直播：
# 龙珠直播：http://star.longzhu.com/wsde135864219
# PPS奇秀直播：https://x.pps.tv/room/208337
# 搜狐千帆直播：https://qf.56.com/520208a
# 来疯直播：https://v.laifeng.com/656428
# LOOK直播：https://look.163.com/live?id=196257915
# AcFun直播：https://live.acfun.cn/live/23682490
# 艺气山直播：http://www.173.com/96
